package com.orange.lswidget.widgets.event.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;

import com.orange.notificationsummary.event.pil.defaultimpl.BaseEvent;
import com.orange.util.LogHelper;

public class MissCallDao extends DaoHelper {
	private static final String	TAG	= "MissCallDao";

	public MissCallDao(Context context) {
		super(context);
	}

	public List<Map<String, String>> queryAllMissCallToList() {
		Cursor cursor = mNotificationInterfc.getMissedCallCursor(mContext);

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		if (cursor != null) {
			try {
				while (cursor.moveToNext()) {
					Map<String, String> messageMap = new HashMap<String, String>();
					int count = cursor.getColumnCount();
					for (int i = 0; i < count; i++) {
						String columnName = cursor.getColumnName(i);
						String columnValue = cursor.getString(i);
						messageMap.put(columnName, columnValue);
						LogHelper.i(TAG, "queryAllMissCallToList. columnName:"
								+ columnName + " ,columnValue: " + columnValue);
					}

					LogHelper.d(TAG, "queryAllMissCallToList. next");
					if (!messageMap.isEmpty()) {
						list.add(messageMap);
					}
				}
			} catch (Exception e) {
				LogHelper.e(TAG, "queryAllMissCallToList. ", e);
			} finally {
				cursor.close();
			}
		}
		return list;
	}

	public Map<String, String> queryLastCallInfo() {
		Map<String, String> callMap = new HashMap<String, String>();
		Cursor cursor = null;
		try {
			cursor = mNotificationInterfc.getMissedCallCursor(mContext);

			while (cursor.moveToNext()) {
				for (int i = 0; i < cursor.getColumnCount(); i++) {
					String columnName = cursor.getColumnName(i);
					String columnValue = cursor.getString(i);
					LogHelper.i(TAG, "getLastCall. columnName: " + columnName
							+ " columnValue: " + columnValue);
					callMap.put(columnName, columnValue);
				}
				return callMap;
			}
		} catch (Exception e) {
			LogHelper.e(TAG, "queryLastCallInfo. ", e);
		} finally {
			if (cursor != null)
				cursor.close();
		}

		return callMap;
	}

	public String[] getMissCallIDsByNumber(String number) {
		return mNotificationInterfc.getMissCallIDsByNumber(number, mContext);
	}

	public boolean updateMissCallStatusByIds(String[] ids) {
		boolean upSuccess = false;
		upSuccess = mNotificationInterfc.updateMissCallStatusByIds(mContext,
				ids);
		return upSuccess;
	}

	public int updateMissCallStatusByNumber(String columnName,
			String columnValue) {
		return mNotificationInterfc.updateMissCallStatusByNumber(mContext,
				columnName, columnValue);
	}

	public List<BaseEvent> queryAllMissedCallEventToList() {
		return mNotificationInterfc.getAllMissedCallEvent(mContext);
	}

	public BaseEvent queryMissCallEvent() {
		List<BaseEvent> missCalls = mNotificationInterfc
				.getAllMissedCallEvent(mContext);

		BaseEvent missCall = null;
		if (missCalls != null && missCalls.size() == 1) {
			missCall = missCalls.get(0);
		}
		return missCall;
	}
}
